$(function () {
    layui.use(['jquery','table'],function(){

        var table = layui.table;
        var $ = layui.jquery;
        //渲染表格
        table.render({
            elem:"#userTable",//渲染的表格的id
            url:"/bbs/getAllUser.action",//请求后台的接口
            cols:[[//表头
                {type:'numbers',title:'编号',width:50,align:'center'},
                {field:'u_id',title:'id',width:120,align:'center'},
                {field:'u_name',title:'姓名',width:180,align:'center'},
                {field:'u_gender',title:'性别',templet:"#gender", width:170,align:'center'},
                {field:'u_email',title:'电子邮件',width:180,align:'center'},
                {field:'u_register_date',title:'注册日期',width:180,align:'center'},
                {field:'u_type',title:'类型',templet:"#type", width:170,align:'center'},
                {title:'操作',toolbar:'#userOperation',width:180,align:'center'},
            ]],
            page:true//是否开启分页
        });

        //表格的重载
        function reloadUserTable(){
            table.reload("userTable",{
                page:{
                    curr:1 //重新从第1页显示
                },
                where:{
                    u_id:$("u_id").val(),
                    u_name:$("u_name").val(),
                    u_gender:$("u_gender").val(),
                    u_email:$("u_email").val(),
                    u_register_date:$("u_register_date").val(),
                    u_type:$("u_type").val(),
                }
            },'data');
        }

        //监听表格的工具栏的事件
        table.on('tool(userTable)',function (obj) {
            var data = obj.data;
            if(obj.event == 'changeUserType'){
                changeUserType(data.u_id);
            }else if(obj.event == 'deleteUser'){
                delUser(data.u_id);
            }else if(obj.event == 'banUser'){
                banUser(data.u_id);
            }
        });

        //升级用户
        function changeUserType(u_id) {
            $.ajax({
                url:"/bbs/changeUserType.action",
                type:"POST",
                data:{'u_id':u_id},
                dataType:'JSON',
                cache:false,
                success:function (data) {
                    if(data.code==200){
                        alert("升级成功!");
                        //重载表格
                        reloadUserTable();
                    }else{
                        alert(data.msg);
                    }
                } ,
                error:function () {
                    alert("升级失败.")
                }
            });
        }

        //删除用户
        function delUser(u_id){
            $.ajax({
                url:"/bbs/deleteUser.action",
                type:"POST",
                data:{'u_id':u_id},
                dataType:'JSON',
                cache:false,
                success:function (data) {
                    if(data.code==200){
                        alert("删除成功!");
                        //重载表格
                        reloadUserTable();
                    }else{
                        alert(data.msg);
                    }
                } ,
                error:function () {
                    alert("删除失败.")
                }
            });
        };

        //禁止用户
        function banUser(u_id){
            $.ajax({
                url:"/bbs/banUser.action",
                type:"POST",
                data:{'u_id':u_id},
                dataType:'JSON',
                cache:false,
                success:function (data) {
                    if(data.code==200){
                        alert("禁止成功!");
                        //重载表格
                        reloadUserTable();
                    }else{
                        alert(data.msg);
                    }
                } ,
                error:function () {
                    alert("禁止失败.")
                }
            });
        };
    });
})

